package com.example.demo.T1.V3;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * T1..
 *
 * @author Lizhong
 * @date 2020/8/18
 */
public class T1 {


    public static void main(String[] args) {

    }


    /**
     * 严格区分"null"与null ,合并null的字段
     *
     * key1="null";
     * key2=null;
     * key3=null;
     *
     * key1!=key2;
     * key2==key3
     *
     * @param arrProA
     * @param arrProB
     * @return
     */
    public static MergedAB[] merge(ProA[] arrProA, ProB[] arrProB) {
        //尽量采用较低的时间复杂度和空间复杂度来实现

        if (arrProA == null || arrProA.length == 0 || arrProB == null || arrProB.length == 0) {
            return new MergedAB[0];
        }

        Map<String, Map<String, List<ProA>>> map = new HashMap<>();
        for (ProA proA : arrProA) {
            if (proA == null) continue;
            map.computeIfAbsent(proA.key1, key -> new HashMap<>())
                    .computeIfAbsent(proA.key2, key -> new ArrayList<>())
                    .add(proA);
        }

        List<MergedAB> mergedABList = new ArrayList<>();
        for (ProB proB : arrProB) {
            if (proB == null) continue;

            Map<String, List<ProA>> innerMap = map.get(proB.key1);
            List<ProA> tempProAList = null;
            if (innerMap == null || (tempProAList = innerMap.get(proB.key2)) == null || tempProAList.isEmpty())
                continue;

            ProA proA = tempProAList.remove(0);
            MergedAB e = new MergedAB();
            e.key1 = proA.key1;
            e.key2 = proA.key2;
            e.v1 = proA.v1;
            e.v2 = proB.v2;
            mergedABList.add(e);
        }

        MergedAB[] mergedABS1 = new MergedAB[mergedABList.size()];
        mergedABList.toArray(mergedABS1);
        return mergedABS1;
    }

}


